package cn.edu.suda.schoolBus.service.impl;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.suda.schoolBus.bean.CarNumber;
import cn.edu.suda.schoolBus.bean.DynamicCarNumber;
import cn.edu.suda.schoolBus.bean.DynamicCarNumberExample;
import cn.edu.suda.schoolBus.dao.DynamicCarNumberMapper;
import cn.edu.suda.schoolBus.service.DynamicCarNumberService;
import cn.edu.suda.schoolBus.util.ResultUtil;

@Service
public class DynamicCarNumberServiceImpl implements DynamicCarNumberService {

	@Autowired
	private DynamicCarNumberMapper dynamicCarNumberDao;

	private DynamicCarNumberExample dynamicCarNumberExample;

	private DynamicCarNumber dynamicCarNumber;

	@Override
	public int addDynamicCarNumber(List<CarNumber> carNumbers) {
		int num = 0;
		Iterator<CarNumber> cIterators = carNumbers.iterator();
		while (cIterators.hasNext()) {
			dynamicCarNumber = new DynamicCarNumber();
			dynamicCarNumber.setRelateToCarNumber(cIterators.next().getCnId());
			num = dynamicCarNumberDao.insertSelective(dynamicCarNumber);
		}
		return num;
	}

	@Override
	public List<DynamicCarNumber> getDynamicCarNumber(Time time) {
		List<DynamicCarNumber> dynamicCarNumbers = dynamicCarNumberDao.selectDynamicCarNumber(time);
		return dynamicCarNumbers;
	}

	@Override
	public int editDynamicCarNumber(int dcnId, int status) {
		dynamicCarNumber = new DynamicCarNumber();
		dynamicCarNumber.setDcnId(dcnId);
		dynamicCarNumber.setDcnStatus(status);
		return dynamicCarNumberDao.updateByPrimaryKeySelective(dynamicCarNumber);
	}

	@Override
	public int destroyDynamicCarNumber() {
		return (destroyDcnStatus() > 0 && destroyDcnMark() > 0) ? 1 : 0;
	}

	private int destroyDcnStatus() {
		dynamicCarNumberExample = new DynamicCarNumberExample();
		dynamicCarNumberExample.createCriteria().andDcnStatusEqualTo(0);
		dynamicCarNumber = new DynamicCarNumber();
		dynamicCarNumber.setDcnStatus(2);
		return dynamicCarNumberDao.updateByExampleSelective(dynamicCarNumber, dynamicCarNumberExample);
	}

	private int destroyDcnMark() {
		dynamicCarNumberExample = new DynamicCarNumberExample();
		dynamicCarNumberExample.createCriteria().andDcnMarkEqualTo(1);
		dynamicCarNumber = new DynamicCarNumber();
		dynamicCarNumber.setDcnMark(0);
		return dynamicCarNumberDao.updateByExampleSelective(dynamicCarNumber, dynamicCarNumberExample);
	}

	@Override
	public DynamicCarNumber getDynamicCarNumber(int dcnId) {
		return dynamicCarNumberDao.selectByPrimaryKey(dcnId);
	}

	@Override
	public int updateDynamicCarNumber(int dcnId, int num) {
		dynamicCarNumber = dynamicCarNumberDao.selectByPrimaryKey(dcnId);
		num += dynamicCarNumber.getCurrentPassengerNumber();
		dynamicCarNumber.setCurrentPassengerNumber(num);
		return dynamicCarNumberDao.updateByPrimaryKeySelective(dynamicCarNumber);
	}

	@Override
	public Map<String, Object> statisticsCarNumber(int days) {
		Date firstDate = new Date();
		Date secondDate = ResultUtil.getDate(days);
		List<DynamicCarNumber> dynamicCarNumbers = dynamicCarNumberDao.statisticsCarNumber(firstDate, secondDate);
		Map<String, Object> map = getMap(dynamicCarNumbers);
		return map;
	}

	private Map<String, Object> getMap(List<DynamicCarNumber> dynamicCarNumbers) {
		String str = null;
		List<String> dTime = new ArrayList<String>();
		List<Integer> pSNumber = new ArrayList<Integer>();
		List<Integer> pFNumber = new ArrayList<Integer>();
		for (DynamicCarNumber dynamicCarNumber : dynamicCarNumbers) {
			str = dynamicCarNumber.getCarNumber().getDepartureTime().toString();
			if (dTime.contains(str)) {
				int index = dTime.indexOf(str);
				if (dynamicCarNumber.getDcnStatus() == 3) {
					pSNumber.set(index, dynamicCarNumber.getCurrentPassengerNumber());
				} else {
					pFNumber.set(index, dynamicCarNumber.getCurrentPassengerNumber());
				}
			} else {
				dTime.add(str);
				if (dynamicCarNumber.getDcnStatus() == 3) {
					pSNumber.add(dynamicCarNumber.getCurrentPassengerNumber());
					pFNumber.add(0);
				} else {
					pFNumber.add(dynamicCarNumber.getCurrentPassengerNumber());
					pSNumber.add(0);
				}
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dTime", dTime);
		map.put("pSNumber", pSNumber);
		map.put("pFNumber", pFNumber);
		return map;
	}

}
